#include<iostream>
using namespace std;
int main(void){
	int arr[500000] = {0}; 
	arr[0] = 1;
	int a,b,c;
	cin>>a>>b>>c;
	int flag = 0;
	int i,j,index;
	int x;
	for(i = 0;flag != 1;i++){
		arr[i+1] = (a * arr[i] + arr[i] % b) % c;
		for(j = 0;j < i + 1;j++){
			if(arr[j] == arr[i+1]){
				flag = 1;
				index = i + 1;
			}
		}
	}
	if(index >= 2000000){
		cout<<-1;
	}else{
		cout<<index;
	} 
	return 0;
}
